using System;
using System.Collections.Generic;

namespace UnitTesting.Fixturing
{
	class EnumerableValueProvider : IValueProvider
	{
		readonly ArrayValueProvider _arrayValueProvider;

		internal virtual int Count
		{
			get { return _arrayValueProvider.Count; }
			set { _arrayValueProvider.Count = value; }
		}

		internal EnumerableValueProvider(IValueProvider valueProvider)
		{
			_arrayValueProvider = new ArrayValueProvider(valueProvider);
		}

		public virtual object Create(Type type)
		{
			Ensure.IsNotNull(() => type);
			if (IsEnumerableType(type))
			{
				var elementType = type.GetGenericArguments()[0];
				return _arrayValueProvider.Create(elementType.MakeArrayType());
			}
			return NotResolved.Value;
		}

		static bool IsEnumerableType(Type type)
		{
			return type.IsGenericType && type.GetGenericTypeDefinition() == typeof (IEnumerable<>);
		}
	}
}